<%@ page contentType="text/html;charset=UTF-8" %>

<!DOCTYPE html>
<html>
<head>
	<title>附件批量录入案例-附件列表</title>
	<%@ include file="/common/import.jsp" %>
    <%@ include file="/common/meta.jsp" %>
    <%@ include file="/js/attachment/common/jquery-swfupload.jsp" %>
	<script>
	//------------------------------------------------------
	   $(function() {
	 $("#uploadDataGrid").datagrid({
		url:'${ctx}/attachment/files/list!data.action',
		title:'附件列表',
		fitColumns:true,
		idField:'sid',
		//width:500,
		//fit:true,
		queryParams:{"objId":"${objId}","objType":"${objType}"},
		width:'auto',
		height:300,
		toolbar:[
			{
				text:'批量删除',
				iconCls:'icon-cancel',
				handler:function(){
					var sels = $("#uploadDataGrid").datagrid("getSelections");
					var sids = "";
					if(sels.length > 0){
						for(var i=0;i<sels.length;i++){
							sids+=sels[i].sid+",";
						}
						delRows(sids);
					}else{
						$.messager.alert("提示","请选择需要删除的记录！","info");
					}
				}
			}
		],
		singleSelect:false,
		onBeforeEdit: function(index, row){
            row.editing = true;
            $('#uploadDataGrid').datagrid('refreshRow', index);
        }, 
		onAfterEdit: function(index, row){
            row.editing = false;
            $('#uploadDataGrid').datagrid('refreshRow', index);
        },
		onCancelEdit: function(index, row){
            row.editing = false;
            $('#uploadDataGrid').datagrid('refreshRow', index);
        },
        frozenColumns:[[
        	{field:'ck',checkbox:true}
        ]],
	 	columns:[[
				{field:'sid',title:'sid',hidden:true},
				{field:'objId',title:'objId',hidden:true},
				{field:'attachmentName',title:'文件名称',width:200,editor:{type:"text",options:{required:true}}},
				{field:'attachmentDesc',title:'文件描述',width:200,editor:{type:"textarea",options:{required:true}}},
				{field:'opt',title:'操作',width:150,formatter:function(value,row,index){
					var da ='<a href="#" onclick="downloada(\''+row.sid+'\')">下载</a>' ;
					if (row.editing) {
                        var s = '<a href="#" onclick="doSubmit(' + index + ')">保存</a> ';
                        var c = '<a href="#" onclick="cancelrow(' + index + ')">取消</a> ';
                        return s + c + da;
                    }
                    else {
                        var e = '<a href="#" onclick="editrow(' + index + ')">编辑</a> ';
                        var d = '<a href="#" onclick="delRow('+row.sid+','+index+')">删除</a> ';
                        return e + d + da ;
                    }
					
				}}
			]]
	 });
	
	  //$("#uploadDataGrid").datagrid('loadData',data);
	  
	        var path = "${ctx}/js/attachment/vinSwfUpload-1.2/";
			var uploadUrl='${ctx}/attachment/files/uploadAttachment.action?createUserId=${as.createUserId}&createUserRealname=${as.createUserRealname}&createSpaceEname=${as.createSpaceEname}';
			/**/$("#vinEdit1").vinSWFUpload({
				uploadURL:uploadUrl,
				width : 1025,
				heigth : 280,
				sizeUnit:"K",
				autoRemove:false,
				filePostName:"file",
				fileLimit:'${attrReg.picTotalNum}',
				maxSize:'${attrReg.maxLength}',
				fileType:"${attrReg.alloweTypes}",
				postParams:{"objId":"${objId}","objType":"${objType}"},
				flashURL : path+"core/SWFUpload.swf",
				addImgURl:path+"images/add.gif",
				uploadImgURl:path+"/images/upload.png",
				stopImgURl:path+"images/stop.png",
				deleteImgURl:path+"images/trash.gif",
				tipImgURl:path+"images/tip.gif"
			});			
	  
});
//------------------------------------------------------
/**
 * 编辑
 * @param {Object} index
 */
function editrow(index){
	$('#uploadDataGrid').datagrid('beginEdit', index);
}
/**
 * 取消
 * @param {Object} index
 */
function cancelrow(index){
    $('#uploadDataGrid').datagrid('cancelEdit', index);
}

	function downloada(sid){
		 window.open("${ctx}/attachment/files/downloadAttachment.action?sid="+sid);
	}

   //取得数据
  function getSelected(){
  	var rec = $('#uploadDataGrid').datagrid('getSelected');
  	if(rec){
  		return rec;
  	}else{
  		$.messager.alert('提示',"请选择需要操作记录!", 'info');
  		return null;
  	}
  }	

/**
 * 删除行
 * @param {Object} rowIndex 行位置
 */
function delRow(id,rowIndex){
	$.messager.confirm('提示','确认删除吗？',function(btn){
		if(btn){
			$("#uploadDataGrid").datagrid("loading");
			$.ajax({
				url:'${ctx}/attachment/files/deleteAtta.action',
				data:{"id":id,"objType":"${objType}","objId":"${objId}"},
				success:function(data){
					var j = $.parseJSON(data);
					$("#uploadDataGrid").datagrid("loaded");
					if(j.flag == "T"){
						$.messager.alert('提示',j.msg,'info',function(){
							//$("#uploadDataGrid").datagrid("deleteRow",rowIndex);
							$("#uploadDataGrid").datagrid("clearSelections");
							$("#uploadDataGrid").datagrid("reload");
							
						});
					}else{
						$.messager.alert('提示',j.msg,'error');
					}
				},
				error:function(){
					$("#uploadDataGrid").datagrid("loaded");
					$.messager.alert('提示',"删除异常请联系管理员！",'error');
				}
			});
		}
	});
}

	

	function loadData(){
	    $('#uploadDataGrid').datagrid("reload");
	}

/**
 * 批量删除
 * @param {Object} sids 行位置
 */
function delRows(sids){
	$.messager.confirm('提示','确认删除吗？',function(btn){
		if(btn){
			$("#uploadDataGrid").datagrid("loading");
			$.ajax({
				url:'${ctx}/attachment/files/deleteAttas.action',
				data:{"ids":sids,"objType":"${objType}","objId":"${objId}"},
				success:function(data){
					var j = $.parseJSON(data);
					$("#uploadDataGrid").datagrid("loaded");
					if(j.flag == "T"){
						$.messager.alert('提示',j.msg,'info',function(){
							//$("#uploadDataGrid").datagrid("deleteRow",rowIndex);
							$("#uploadDataGrid").datagrid("clearSelections");
							$("#uploadDataGrid").datagrid("reload");
							
						});
					}else{
						$.messager.alert('提示',j.msg,'error');
					}
				},
				error:function(){
					$("#uploadDataGrid").datagrid("loaded");
					$.messager.alert('提示',"删除异常请联系管理员！",'error');
				}
			});
			
		}
	});
}

 /**
 * 提交
 * @param {Object} index
 */
function doSubmit(index){
	//结束编辑
	$('#uploadDataGrid').datagrid('endEdit',index);
	//取到需要提交的值
	var row =  $('#uploadDataGrid').datagrid('getRows')[index];
	
	$('#uploadDataGrid').datagrid("loading");
	$.ajax({
		type : "post",//请求方式
		url : "${ctx}/attachment/files/updateAtta.action",//发送请求地址
		data : row,
		//请求成功后的回调函数有两个参数
		success : function(data) {
			$('#uploadDataGrid').datagrid("loaded");
			var d = $.parseJSON(data);
			if(d.flag == 'T'){
				$.messager.alert("提示",d.msg,"info");
			}else{
				$.messager.alert("提示",d.msg,"error");
			}
		},
		error:function(){
			$('#uploadDataGrid').datagrid("loaded");
			$.messager.alert("提示","修改失败！请联系管理员","error");
		}
	});

	
	
} 
	//------------------------------------------------------
	
	
	function clickAttaSort(type){
	   if(type=="1"){
	     window.location.href="${ctx}/attachment/files/list.action?objId=${objId}&objType=${objType}";
	   }
	}
	
	</script>
	<style>
	
</style>
</head>
<body>
<div style="padding:5px 0;">
	<span>				  
	 <input type="radio" name="attaSort" value="0"  checked=checked onclick="clickAttaSort('0')" />&nbsp;附件
	 <input type="radio" name="attaSort" value="1" onclick="clickAttaSort('1')" />&nbsp;图片
	</span>	&nbsp;&nbsp;
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" onclick="javascript:loadData();" >刷新列表</a>
	</div>
 <table  id="uploadDataGrid"></table> 

    <!-- 上传窗口 -->
	<div id="saveDiv" class="easyui-window" title="附件批量上传" style="padding:5px;width: 700px;height:380px;" 	
    	iconCls="icon-edit" closed="true" maximizable="false" minimizable="false" collapsible="false">
   		<iframe frameborder="0"  id="saveFrame" height="100%" width="100%" scrolling="No" frameborder="0" ></iframe>
    </div>

	<div title="附件上传" class="easyui-panel">
		<div id="vinEdit1" style="width:100%"></div>
	</div>

</body>
</html>